package com.java.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.pattern.IntegerPatternConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.java.po.Admin;
import com.java.po.Student;
import com.java.service.AdminService;
import com.java.service.LogService;
import com.java.service.StudentService;
import com.java.util.LogUtil;
import com.java.util.PageUtil;

@Controller
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private AdminService adminService;

	@Autowired
	private StudentService studentService;

	@Autowired
	private LogService logService;

	// 全局变量
	LogUtil logUtil = new LogUtil();

	// 报到首页界面
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() {

		return "views/admin/admin_index";

	}

	// 登录界面
	@RequestMapping(value = "/adminLogin", method = RequestMethod.GET)
	public String login(@RequestParam(name = "out", defaultValue = "") String out, HttpServletRequest request) {
		// 退出登陆
		if (out.length() != 0) {
			request.getSession().invalidate(); // 杀死所有session
			return "redirect:adminLogin";
		}
		return "loginAndregister";

	}

	/**
	 * list界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/listView", method = RequestMethod.GET)
	public String listStudent(@RequestParam(name = "tname", defaultValue = "") String tname,
			@RequestParam(name = "className", defaultValue = "") String className,
			@RequestParam(name = "tid", defaultValue = "") String tid,
			@RequestParam(name = "state", defaultValue = "") String state,
			@RequestParam(name = "find", defaultValue = "") String view, Model map,
			@RequestParam(name = "page", defaultValue = "1") String currentPage) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		try {
			switch (view) {
			case "listStudent":
				queryMap.put("username", tname);
				System.out.println(tid);
				if (tid.length() != 0 && !tid.equals("") && !tid.equals("undefined")) {
					queryMap.put("studentid", Integer.parseInt(tid));
				}
				queryMap.put("state", state);
				queryMap.put("inclass", className);
				int page = 0;// 页码
				// 判断是否为第一次，如果是第一次，pageNo是一个空值
				if ("".equals(currentPage) || null == currentPage) {
					page = 1; // 把页码设为第一页
				} else {
					try {
						page = Integer.parseInt(currentPage);// 传过来的是字符串
					} catch (Exception e) {
						// TODO: handle exception
						page = 1;// 转换，把字符串转换成整数
					}
				}
				PageUtil<Student> findPageStudent = studentService.findPageStudent(queryMap, page);
				map.addAttribute("findPageStudent", findPageStudent);
				map.addAttribute("currentPage", page);
				break;

			default:
				break;
			}

		} catch (Exception e) {
			// TODO: handle exception
			map.addAttribute("type", "error");
			map.addAttribute("msg", "发生未知错误！请联系管理员");
			e.printStackTrace();
		}

		return "views/admin/" + view;

	}

	/**
	 * 登陆提交
	 * 
	 * @param username
	 * @param adminid
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/tAdminLogin", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> tlogin(@RequestParam(name = "username2", defaultValue = "") String username,
			@RequestParam(name = "password2", defaultValue = "") String password,
			@RequestParam(name = "vcode2", defaultValue = "") String vcode, HttpServletRequest request) {
		Map<String, String> retMap = new HashMap<String, String>();
		try {
			if (username.length() != username.trim().length()) {
				retMap.put("type", "error");
				retMap.put("msg", "请输入正确的用户名，首部或者尾部存在空格");
				return retMap;
			}
			if (username == null || username.length() == 0) {
				retMap.put("type", "error");
				retMap.put("msg", "用户名不能为空！");
				return retMap;
			}
			if (password == null || password.trim().length() == 0) {
				retMap.put("type", "error");
				retMap.put("msg", "密码不能为空！");
				return retMap;
			}
			Object attribute = request.getSession().getAttribute("accountLoginCpacha");
			if (attribute == null) {
				retMap.put("type", "error");
				retMap.put("msg", "验证码过期，请刷新！");
				return retMap;
			}
			if (!vcode.equalsIgnoreCase(attribute.toString())) {
				retMap.put("type", "error");
				retMap.put("msg", "验证码错误！");
				return retMap;
			}
//			Admin admin = adminService.findByNameAndStuId(null,username, 
//					MD5Util.md5Password(password));
			Admin admin = adminService.findByNameAndStuId(null, username, password);
			if (admin == null) {
				retMap.put("type", "error");
				retMap.put("msg", "用户不存在！或者密码有误。");
				return retMap;
			}
			if (admin.getEnble() == 0) {
				retMap.put("type", "error");
				retMap.put("msg", "该用户已被禁用，请联系管理员！");
				logService.insertLog(username, logUtil.getLOGIN(), logUtil.getERROR());
				return retMap;
			}
			request.getSession().setAttribute("admin", admin);
			retMap.put("type", "success");
			retMap.put("msg", "欢迎进入报到系统后台！");
			logService.insertLog(username, logUtil.getLOGIN(), logUtil.getSUCCESS());
		} catch (Exception e) {
			retMap.put("type", "error");
			retMap.put("msg", "发生未知错误！请联系管理员");
			e.printStackTrace();
		}

		return retMap;

	}

	/**
	 * 查询一条数据
	 * 
	 * @param list
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/findObject", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findlist(@RequestParam(name = "tid", defaultValue = "") String tid) {
		Map<String, Object> retMap = new HashMap<String, Object>();
		try {
			switch (tid) {
			case "0":

				break;
			case "1":
				Student findByNameAndStuId = studentService.findByNameAndStuId(null, Integer.parseInt(tid), null);
				retMap.put("student", findByNameAndStuId);
				break;
			case "2":
				break;
			case "3":

				break;
			case "4":

				break;

			default:
				break;
			}
		} catch (Exception e) {
			// TODO: handle exception
			retMap.put("type", "error");
			retMap.put("msg", "发生未知错误！请联系管理员");
			e.printStackTrace();
		}

		return retMap;
	}

	@RequestMapping(value = "/insertStudent", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, String> insert(Student student) {
		Map<String, String> retMap = new HashMap<String, String>();
		try {
			int insertStudentOne = studentService.insertStudentOne(student);
			if (insertStudentOne!=1) {
				System.out.println("添加失败");
			}
		} catch (Exception e) {
			// TODO: handle exception
			retMap.put("type", "error");
			retMap.put("msg", "发生未知错误！请联系管理员");
			e.printStackTrace();
		}

		return retMap;
	}
}
